from ctypes.wintypes import tagPOINT
from django.shortcuts import render

# Create your views here.
from django.http import JsonResponse
from django.views.decorators.http import require_http_methods
from django.core import serializers
# 认证模块
from django.contrib import auth
# 对应数据库
from django.contrib.auth.models import User
from django.contrib.auth import *
import requests
import json
# 返回用户信息
@require_http_methods(["GET"])
def getUserList(request):
    response = {}
    try:
        if(request.GET.get('body') != 'undefined'):
            body = json.loads(request.GET.get('body'))
            if(body['searchType'] == 'username'):
                response['userInf'] = list(User.objects.all().filter(username__icontains = body['searchData']).values())
            elif(body['searchType'] == 'first_name'):
                response['userInf'] = list(User.objects.all().filter(first_name__icontains = body['searchData']).values())
        else:
            response['userInf'] = list(User.objects.all().values())
        response['msg'] = 'success'
        response['error_num'] = 0
    except  Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)

# 删除用户
@require_http_methods(["GET"])
def handleDelete(request):
    response = {}
    try:
        body = json.loads(request.GET.get('body'))
        User.objects.filter(
            username = body['username'],
            first_name = body['first_name'],
            email = body['email']
        ).delete()

        response['msg'] = 'success'
        response['error_num'] = 0
    except  Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)

# 修改用户信息
@require_http_methods(["GET"])
def handleEdit(request):
    response = {}
    try:
        body = json.loads(request.GET.get('body'))
        User.objects.filter(
            username = body['oldUsername'],
            first_name = body['oldFirst_name'],
            email = body['oldEmail']
        ).update(
            username = body['newUsername'],
            first_name = body['newFirst_name'],
            email = body['newEmail'],
            is_superuser = body['newIs_superuser']
        )
        response['msg'] = 'success'
        response['error_num'] = 0
    except  Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)

# 获取个人信息
@require_http_methods(["GET"])
def getPersonalInf(request):
    response = {}
    try:
        body = json.loads(request.GET.get('body'))
        response['personalInf'] = list(User.objects.all().filter(username = body['username']).values())
        response['msg'] = 'success'
        response['error_num'] = 0
    except  Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)

# 修改个人信息
@require_http_methods(["GET"])
def editPersonalInf(request):
    response = {}
    try:
        body = json.loads(request.GET.get('body'))
        user = authenticate(
            username = body['oldUsername'],
            password = body['oldPassword'],
        )
        if user is not None:
            User.objects.filter(
                username = body['oldUsername'],
            ).update(
                username = body['username'],
                first_name = body['first_name'],
                email = body['email'],
            )
            response['msg'] = 'success'
            response['error_num'] = 0
        else:
            response['msg'] = str('旧密码错误，请重新输入!')
            response['error_num'] = 1
    except  Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 1
    return JsonResponse(response)

